package top.minishow.pdf.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @author hhewanning
 * @date 2019/3/2
 * @description 文件流处理的工具类
 */
public class FileUtil {

    public static boolean write(MultipartFile multipartFile, String targetFilePath) {
        InputStream inputStream = null;
        try {
            inputStream = multipartFile.getInputStream();
            write(inputStream, targetFilePath);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * @param inputStream
     * @param targetFilePath
     * @return boolean
     * @author hewanning
     * @date 2019/3/2
     * @description 将流写到目标位置
     * @modifier
     */
    public static boolean write(InputStream inputStream, String targetFilePath) {
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(targetFilePath);
            int len;
            int offset = 0;
            int byteValue = 1024;
            byte[] buf = new byte[byteValue];
            while ((len = inputStream.read(buf, offset, byteValue)) != -1) {
                fileOutputStream.write(buf, offset, len);
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    public static String getFileName(String fullFilePath) {
        int lastIndexOf = fullFilePath.lastIndexOf("/");
        String fileName = fullFilePath.substring(lastIndexOf + 1);
        fileName = fileName.replace("." + getFileSuffix(fullFilePath), "");
        return fileName;
    }

    public static String getFileSuffix(String fullFilePath) {
        if (fullFilePath == null && fullFilePath.length() < 1) {
            return null;
        }
        int lastIndexOf = fullFilePath.lastIndexOf(".");
        return fullFilePath.substring(lastIndexOf + 1);
    }

    public static void write(String htmlPath, String htmlBody) {
        File file = new File(htmlPath);
        File parent = file.getParentFile();
        // 如果pdf保存路径不存在，则创建路径
        if (!parent.exists()) {
            parent.mkdirs();
        }
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(htmlPath, "UTF-8");
            writer.println(htmlBody);
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

}
